//
//  ItemForGuoWai.m
//  
//
//  Created by dllo on 16/1/27.
//
//

#import "ItemForGuoWai.h"
#import "PCH.pch"
#import "CellForGuoWai.h"
#import "ModelForGuoWai.h"
#import <UIImageView+WebCache.h>


@interface ItemForGuoWai () <UICollectionViewDataSource, UICollectionViewDelegate>

@property (nonatomic, retain)UICollectionView *collectionView;
@property (nonatomic, retain)UICollectionViewFlowLayout *layout;
@property (nonatomic, retain)NSMutableArray *arrayOfModel;

@end

@implementation ItemForGuoWai

- (void)dealloc {
    self.collectionView.delegate = nil;
    self.collectionView.dataSource = nil;
    [_arrayOfModel release];
    [_layout release];
    [_collectionView release];
    [super dealloc];
}

- (instancetype)initWithFrame:(CGRect)frame {
    
    self = [super initWithFrame:frame];
    if (self) {
        self.arrayOfModel = [NSMutableArray array];
        
        self.layout = [[UICollectionViewFlowLayout alloc] init];
        self.layout.minimumInteritemSpacing = 20;
        self.layout.minimumLineSpacing = 20;
        self.layout.scrollDirection = UICollectionViewScrollDirectionVertical;
        self.layout.sectionInset = UIEdgeInsetsMake(20, 20, 20, 20);
        self.layout.itemSize = CGSizeMake((self.contentView.frame.size.width - 60) / 2.0, (self.contentView.frame.size.width - 60) / 2 * 1.15);
      
        self.collectionView = [[UICollectionView alloc] initWithFrame:self.contentView.frame collectionViewLayout:self.layout];
        [self.contentView addSubview:self.collectionView];
        [_collectionView release];
        self.collectionView.backgroundColor = [UIColor whiteColor];
        self.collectionView.delegate = self;
        self.collectionView.dataSource = self;
        self.collectionView.dk_backgroundColorPicker = DKColorWithRGB(0xffffff, 0x343434);
        
        [self.collectionView registerClass:[CellForGuoWai class] forCellWithReuseIdentifier:@"pool"];
        
#pragma mark - 解析数据
        [NetworkingHandler networkingWithURL:@"http://chanyouji.com/api/destinations.json?page=1" completeHandle:^(NSData *data,id result) {
            
           NSArray *array = [[result objectAtIndex:0] objectForKey:@"destinations"];
            for (NSDictionary *dic in array) {
                
                ModelForGuoWai *model = [[ModelForGuoWai alloc] init];
                [model setValuesForKeysWithDictionary:dic];
                [self.arrayOfModel addObject:model];
                [model release];
               
            }
            
           NSArray *array2 = [[result objectAtIndex:1] objectForKey:@"destinations"];
            for (NSDictionary *dic in array2) {
                ModelForGuoWai *model = [[ModelForGuoWai alloc] init];
                [model setValuesForKeysWithDictionary:dic];
                [self.arrayOfModel addObject:model];
                [model release];
                
            }
            
            NSArray *array3 = [[result objectAtIndex:2] objectForKey:@"destinations"];
            for (NSDictionary *dic in array3) {
                ModelForGuoWai *model = [[ModelForGuoWai alloc] init];
                [model setValuesForKeysWithDictionary:dic];
                [self.arrayOfModel addObject:model];
                [model release];
            }
            
            [self.collectionView reloadData];
            
        }];
        
    }
    
    return self;
}

#pragma mark - collectionView协议方法
/** 返回item个数 */
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.arrayOfModel.count;
}

/** 返回cell */
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    CellForGuoWai *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"pool" forIndexPath:indexPath];
    [cell passModel:[self.arrayOfModel objectAtIndex:indexPath.item]];
    
    return cell;
    
}

#pragma mark - 选中item
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    
    ModelForGuoWai *model = [self.arrayOfModel objectAtIndex:indexPath.item];
    
    [[NSNotificationCenter defaultCenter] postNotificationName:@"passID" object:nil userInfo:@{@"ID" : model.id, @"name" : model.name_zh_cn}];
    
}

#pragma mark - 布局子视图
- (void)applyLayoutAttributes:(UICollectionViewLayoutAttributes *)layoutAttributes {
    
    self.collectionView.frame = self.contentView.frame;
    
}


























@end
